package view;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractAction;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import model.HighScore;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class HighScoreView extends JFrame implements KeyListener {
    private static final long serialVersionUID = 2608023482884436717L;
    private JLabel labelHighScore;
    private AbstractAction okButtonAction;
    private JTextArea hsTextField;
    private JButton okButton;
    private HighScore highScore;
    private String hsList;

    public HighScoreView() {
        highScore = new HighScore();
        try {
            hsList = highScore.readFromFile();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        initGUI();
        this.setVisible(true);
        this.setResizable(false);
        this.setLocationRelativeTo(this.getParent());
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setFocusable(true);
        addKeyListener(this);

    }

    private void initGUI() {
        try {
            {
                getContentPane().setLayout(null);
                getContentPane().setBackground(new java.awt.Color(255, 255, 255));
                this.setTitle("High Score");
                this.setPreferredSize(new java.awt.Dimension(400, 600));
                {
                    labelHighScore = new JLabel();
                    getContentPane().add(labelHighScore);
                    labelHighScore.setText("High Score");
                    labelHighScore.setBounds(141, 20, 90, 25);
                    labelHighScore.setFont(new java.awt.Font("Segoe UI", 1, 16));
                }
                {
                    okButton = new JButton();
                    getContentPane().add(okButton);
                    getContentPane().add(getHsTextField());
                    okButton.setBounds(318, 525, 55, 26);
                    okButton.setAction(getOkButtonAction());
                    okButton.setText("OK");
                }
                this.setSize(400, 600);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("serial")
    private AbstractAction getOkButtonAction() {
        if(okButtonAction == null) {
            okButtonAction = new AbstractAction("OK", null) {
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            };
        }
        return okButtonAction;
    }

    private JTextArea getHsTextField() {
        if(hsTextField == null) {
            hsTextField = new JTextArea();
            hsTextField.setBounds(12, 66, 361, 440);
            hsTextField.setEditable(false);
            hsTextField.setPreferredSize(new java.awt.Dimension(361, 440));
            hsTextField.setFont(new java.awt.Font("Segoe UI", 0, 14));
            hsTextField.setText(hsList);
        }
        return hsTextField;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        Character c = e.getKeyChar();
        if(c.equals('\n')) {
            okButton.doClick();
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public void keyTyped(KeyEvent e) {}
}
